package org.csu.lestore.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.csu.lestore.api.common.CommonResponse;
import org.csu.lestore.api.entity.Cart;
import org.csu.lestore.api.service.AccountService;
import org.csu.lestore.api.service.CartService;
import org.csu.lestore.api.service.CatalogService;
import org.csu.lestore.api.vo.AccountVO;
import org.csu.lestore.api.vo.CartVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/cart/")
public class CartController {
    //创建Logger对象
    private static Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private CartService cartService;
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private AccountService accountService;

    @GetMapping("cartItem")//获取登陆账号的购物车
    @ResponseBody
    public CommonResponse<IPage<CartVO>> getCartItemList(HttpSession session){
        AccountVO login_accountVO = (AccountVO) session.getAttribute("login_account");
        if (login_accountVO!=null) {
            return cartService.getCartListInPage(1,2147483647,login_accountVO.getUserId());
        }
        else{
            return CommonResponse.createForError("未登录");
        }
    }

    @PostMapping("insertCartItem")//商品加入购物车
    @ResponseBody
    public CommonResponse<Cart> insertCartItem(@RequestParam("itemId") int itemId,
                                               @RequestParam("itemCount") int itemCount,
                                               HttpSession session){
        AccountVO login_accountVO = (AccountVO) session.getAttribute("login_account");
        if (login_accountVO!=null) {
            Cart cartItem = new Cart();
            cartItem.setUserId(login_accountVO.getUserId());
            cartItem.setItemId(itemId);
            cartItem.setItemCount(itemCount);
            return cartService.insertCartItem(cartItem);
        }
        else{
            return CommonResponse.createForError("未登录");
        }
    }

    @PostMapping("updateCartItem")//更新购物车商品数量
    @ResponseBody
    public CommonResponse<Cart> updateCartItem(@RequestParam("itemId") int itemId,
                                               @RequestParam("itemCount") int itemCount,
                                               HttpSession session){
        AccountVO login_accountVO = (AccountVO) session.getAttribute("login_account");
        if (login_accountVO!=null) {
            Cart cartItem = new Cart();
            cartItem.setUserId(login_accountVO.getUserId());
            cartItem.setItemId(itemId);
            cartItem.setItemCount(itemCount);
            return cartService.updateCartItem(cartItem);
        }
        else{
            return CommonResponse.createForError("未登录");
        }
    }

    @PostMapping("deleteCartItem")//删除
    @ResponseBody
    public CommonResponse<Cart> deleteCartItem(@RequestParam("itemId") int itemId,
                                               HttpSession session){
        AccountVO login_accountVO = (AccountVO) session.getAttribute("login_account");
        if (login_accountVO!=null) {
            Cart cartItem = new Cart();
            cartItem.setUserId(login_accountVO.getUserId());
            cartItem.setItemId(itemId);
            return cartService.deleteCartItem(cartItem);
        }
        else{
            return CommonResponse.createForError("未登录");
        }
    }

    @PostMapping("clearCart")//清空
    @ResponseBody
    public CommonResponse<Cart> clearCart(HttpSession session){
        AccountVO login_accountVO = (AccountVO) session.getAttribute("login_account");
        if (login_accountVO!=null) {
            return cartService.clearCartItemByUserId(login_accountVO.getUserId());
        }
        else{
            return CommonResponse.createForError("未登录");
        }
    }
}
